/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Laurent Berthelot
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display.screen
{
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.EventDispatcher;
	import flash.events.FullScreenEvent;
	import flash.geom.Rectangle;

	/**
	 * Dispatched when the Stage object enters, or leaves, full-screen mode.
	 * @eventType flash.events.FullScreenEvent.FULL_SCREEN
	 */
	[Event(name = "fullScreen", type = "flash.events.FullScreenEvent")]
	
	/**
	 * FullScreenManager class used to manipulate fullscreen display
	 * @author	Laurent Berthelot
	 */
	public class FullScreenManager extends EventDispatcher
	{
		//--------------------------------------------------------------------------
		//
		//  Private vars
		//
		//--------------------------------------------------------------------------
		private var _stage:Stage;
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Constructs a new FullscreenManager instance
		 * @param	stage	A reference to the stage object needed to set the stageDisplayState
		 */
		public function FullScreenManager(stage:Stage) {
			_stage = stage;
			_stage.addEventListener(FullScreenEvent.FULL_SCREEN, _onDisplayStateChange);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Public methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Set or unset fullscreen display
		 * @param	fullscreen indicates if fullscreen must be active (true) or inactive (false)
		 * @param	sourceRect	the rectangle that should be scaled to fullscreen. If null (default) the whole stage is used.
		 */
		public function setFullScreen(fullscreen:Boolean = true, sourceRect:Rectangle = null):void {
			if (fullscreen) {
				_stage.fullScreenSourceRect = sourceRect;
				_stage.displayState = StageDisplayState.FULL_SCREEN;
				return;
			}
			_stage.displayState = StageDisplayState.NORMAL;
		}
		
		/**
		 * Toogles the fullscreen display
		 */
		public function toggle():void {
			setFullScreen(_stage.displayState === StageDisplayState.NORMAL);
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Events
		//
		//--------------------------------------------------------------------------
		private function _onDisplayStateChange(e:FullScreenEvent):void {
			dispatchEvent(e);
		}
	}
}